package prefix;
import java.util.*;
import java.io.*;

public class PrefixListOperation {
	public static void writePrefixList(ArrayList<Prefix> pl){
		String filename = new String("PrefixList.txt");
		try{
			BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
			for(int i = 0; i < pl.size(); i++){
				bw.write(""+i+":");
				Prefix pi = pl.get(i);
				for(int j = 0; j < pi.itemList.size(); j++){
					bw.write("\t"+pi.itemList.get(j));
				}
				bw.newLine();
			}
			bw.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	public static void writeStorySpace(ArrayList<Prefix> pl, String filename){
		if(filename == null)
			filename = new String("GridStorySpace.txt");
		try{
			BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
			for(int i = 0; i < pl.size(); i++){
//				bw.write(""+i+":");
				Prefix pi = pl.get(i);
				bw.write(""+pi.itemList.get(0));
				for(int j = 1; j < pi.itemList.size(); j++){
					bw.write("\t"+pi.itemList.get(j));
				}
				bw.newLine();
			}
			bw.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
//	type: 1 integer plot point	
	public static ArrayList<Prefix> readPrefixList(String filename, int type){
		ArrayList<Prefix> pl = new ArrayList<Prefix>();
		if(type == 1){
			try{
				BufferedReader br = new BufferedReader(new FileReader(filename));
				String line;
				while((line = br.readLine()) != null){
					String[] t = line.split("\t");
					int[] list = new int[t.length - 1];
					for(int i = 1; i < t.length; i++){
						list[i-1] = Integer.parseInt(t[i]);
					}
					Prefix pi = new Prefix(list);
					pl.add(pi);
				}
	
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
		
		return pl;
		
	}
	

}
